package com.vhall.component.controller.account.console;

import com.alibaba.fastjson.JSONObject;
import com.vhall.component.controller.account.facade.console.CslAuthFacade;
import com.vhall.component.entity.account.vo.*;
import com.vhall.component.entity.rbac.vo.RolePermissionConsoleRequestVO;
import com.vhall.component.entity.rbac.vo.RolePermissionConsoleResponseVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.account.AccountService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 控制台用户 - 登录、登出、获取登录用户详情
 *
 * @author dalong
 * @date 2021-04-25 16:46
 */
@Api(tags = "CslAuthController", value = "控制台用户")
@VhallComponent
public class CslAuthController implements CslAuthFacade {

    @Autowired
    private AccountService accountService;

    @PostMapping("/login")
    @Override
    public AuthLoginRspVO login(@Validated AuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.authLogin(authLoginVO, request, response, true);
    }

    @Override
    public AuthLoginRspVO assistantLogin(AuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.authLogin(authLoginVO, request, response, false);
    }

    @Override
    public AuthLoginRspVO loginSso(AuthConsoleLoginSsoVO vo) {
        return accountService.loginSso(vo);
    }

    @Override
    @TokenAuth
    public void logout(@Validated AuthLogoutVO authLogoutVO) {
        accountService.authLogout(authLogoutVO);
    }

    @Override
    @TokenAuth
    public JSONObject permission() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        Token token = TokenThreadLocal.getToken();

        return accountService.permission(token.getAccountId(), request, response);
    }
    @TokenAuth
    @Override
    public List<RolePermissionConsoleResponseVO> rolePermissionConsole(RolePermissionConsoleRequestVO requestVO) {
        return accountService.rolePermissionConsole(requestVO);
    }
    @TokenAuth
    @Override
    public ChangeAccountRoleResponseVO changeAccountRole(ChangeAccountRoleRequestVO vo) {
        return accountService.changeAccountRole(vo);
    }
}
